package in.blogspot.khurram2java;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Shader.TileMode;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

//search method yoyo palam
public class Search extends Activity {
    EditText editText1;
    ListView listView1;
    private String[] title;
    DatabaseHelper myDB = new DatabaseHelper(this);
    
    ArrayList<BookData> arr_list;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        listView1 = (ListView)findViewById(R.id.listView1);
        
                
        
        arr_list = new ArrayList<BookData>();
        arr_list = myDB.SelectAllData();
        
        title = new String[arr_list.size()];
        
        /*
        for (BookData mem : arr_list){
        System.out.println(mem.getTitle());
        }
        */

        for(int i=0; i < arr_list.size(); i++){
        	title[i] = arr_list.get(i).getTitle();
        }
        
        listView1.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, title));
        
        editText1 = (EditText)findViewById(R.id.editText1);
        editText1.addTextChangedListener(new TextWatcher() {
			public void afterTextChanged(Editable arg0) {
	        	ArrayList<String> src_list = new ArrayList<String>();
	        	
				int textlength = editText1.getText().length();
				String[] src_title ;
				for(int i = 0 ; i < title.length ; i++){
					try {
						if(editText1.getText().toString().equalsIgnoreCase((String)title[i].subSequence(0, textlength))){
							src_list.add(title[i]);
						}
					} catch (Exception e) { }
	        	}
				src_title=src_list.toArray(new String[src_list.size()]);
				listView1.setAdapter(new ArrayAdapter(Search.this, android.R.layout.simple_list_item_1,src_title ));
			}
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) { }
			public void onTextChanged(CharSequence s, int start, int before,
					int count) { }
        	
        });
    }
}